﻿Ext.gesture.Drag = Ext.extend(Ext.gesture.Touch, {
    handles: ['dragstart', 'drag', 'dragend'],

    dragThreshold: 5,//手指滑动大于或等于5个像素算拖动

    direction: 'both',

    horizontal: false,
    vertical: false,

    constructor: function () {
        Ext.gesture.Drag.superclass.constructor.apply(this, arguments);

        if (this.direction == 'both') {
            this.horizontal = true;
            this.vertical = true;
        }
        else if (this.direction == 'horizontal') {
            this.horizontal = true;
        }
        else {
            this.vertical = true;
        }

        return this;
    },

	//触摸屏幕开始时刻触发
    onTouchStart: function (e, touch) {
        this.startX = this.previousX = touch.pageX;//记录开始点横坐标
        this.startY = this.previousY = touch.pageY;//记录开始点纵坐标
        this.startTime = this.previousTime = e.timeStamp;//开始时间戳

        this.dragging = false;//拖拽进行时的标志
    },

	//触摸点滑动时刻触发
    onTouchMove: function (e, touch) {
        if (this.isLocked('drag')) {
            return;
        }

        var info = this.getInfo(touch);//获取事件详细信息（touch为手机事件中的e.changedTouches中的事件）

		//如果不是正在滑动
        if (!this.dragging) {
            if (this.isDragging(info) && this.fire('dragstart', e, info)) {//如果拖拽开始，触发dragstart事件
                this.dragging = true;//拖拽进行时标记
                this.lock('drag', 'dragstart', 'dragend');//死锁三事件
                this.fire('drag', e, info);//触发drag事件
            }
        }
        else {
            this.fire('drag', e, info);//触发drag事件
        }
    },

	//手指移开时触发
    onTouchEnd: function (e) {
        if (this.dragging) {//如果在拖拽中
            this.fire('dragend', e, this.lastInfo);//触发'dragend'事件
        }

        this.dragging = false;//拖拽进行时标记设为false
    },

    isDragging: function (info) {
        return (
            (this.horizontal && info.absDeltaX >= this.dragThreshold) ||
            (this.vertical && info.absDeltaY >= this.dragThreshold)
        );
    },


    isVertical: function () {
        return this.vertical;
    },


    isHorizontal: function () {
        return this.horizontal;
    }
});

Ext.regGesture('drag', Ext.gesture.Drag);